<template>
  <div class="yy-login">

    <div class="yy-login-form">
      <div class="yy-login-title">智慧分诊叫号管理平台</div>
      <el-form :model="loginForm" label-position="top" label-width="0px">
        <el-form-item>
          <el-input size="large" prefix-icon="user" v-model.trim="loginForm.username"  placeholder="请输入用户名">
          </el-input>
        </el-form-item>

        <el-form-item>
          <el-input size="large" prefix-icon="Lock"  type="password" v-model.trim="loginForm.password"  placeholder="请输入密码">
          </el-input>
        </el-form-item>

        <el-form-item style="margin-top: 45px;">
          <el-button size="large" type="primary" style="width: 100%;" :disabled="isLogin" @click="onLogin">{{isLogin?'登录中...':'登  录'}}</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="yy-login-footer">
      <span>Copyright © 2024 上海丫乙科技有限公司</span>
    </div>
  </div>
</template>
<script setup lang="ts">

import {reactive, ref} from "vue";
import {useRoute, useRouter} from "vue-router";
import {ElMessage} from "element-plus";
import useUserStore from "@/store/modules/user";
import { login, deviceLogin, getInfo } from '@/api/login'
import StorageUtils from "@/utils/StorageUtils";
import {sm2} from "sm-crypto";

const userStore = useUserStore()

const route = useRoute()
const router = useRouter()
const isLogin = ref(false)
const loginForm = reactive({
  username: '',
  password: ''
})

const onLogin = () => {
  if (!loginForm.username || !loginForm.password) {
    ElMessage.warning({
      showClose: true,
      message: '请输入账号和密码'
    });
    return
  }

  isLogin.value = true
  deviceLogin(loginForm.username, '04' + sm2.doEncrypt(loginForm.password, import.meta.env.VITE_APP_SM2_PUBLICKEY)).then((res) => {
    console.log("----------------login result----", res)
    if (res.code === 200) {
      StorageUtils.setUserLoginTokenStorage(res.data.token)
      router.push({name: 'DoctorClient', replace: true})
    }
    isLogin.value = false
  }, () => {
    isLogin.value = false
  })
}

</script>
<style scoped lang="scss">
.yy-login {
  display: flex;
  width: 100%;
  height: 100%;
  position: relative;
  background: #f1f1f1 url("../../assets/images/login-background01.jpg");
  background-size: cover;
  &-title {
    font-size: 18px;
    text-align: center;
    margin-bottom: 30px;
  }
  &-form {
    border-radius: 6px;
    background: #ffffff;
    width: 400px;
    padding: 25px 25px 5px 25px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);

  }
  &-footer {
    height: 40px;
    line-height: 40px;
    position: fixed;
    bottom: 0;
    width: 100%;
    text-align: center;
    color: #fff;
    font-family: Arial;
    font-size: 12px;
    letter-spacing: 1px;
  }
}
</style>
